package root.electronclass.electronclass.grade.notice

import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import root.electronclass.electronclass.R
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentClassListBinding
import root.electronclass.electronclass.databinding.FragmentClassNotificationParentItemBinding
import root.electronclass.electronclass.ext.CoroutineExceptionHandler
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.ext.tabs
import root.electronclass.electronclass.ext.toMMddnHHmm
import root.electronclass.electronclass.grade.model.ClassNotification
import root.electronclass.electronclass.mvp.rest.ClassApi
import root.electronclass.electronclass.other.WebFragment

/**
 * 班级通知
 * @author Lokiy
 * @date 2023/2/4 13:05
 */
class ClassNotificationFragment : ViewBindingFragment<FragmentClassListBinding>() {

    private val viewModel: ClassNotificationViewModel by viewModels()

    private val adapter = BasicAdapter<ClassNotification, FragmentClassNotificationParentItemBinding>({ viewModel.load() }) { holder, item ->
        holder.binding.vDash.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = if (holder.absoluteAdapterPosition == 0) 12 else 0
        }
        holder.binding.itemTitle.text = item.createTime.toMMddnHHmm
        holder.binding.include.itemTitle.text = item.title
        holder.binding.include.itemStatus.isSelected = item.status == 2
        holder.binding.include.itemStatus.let {
            it.isSelected = item.status == 2
            it.text = if (it.isSelected) getString(R.string.read) else getString(R.string.unread)
        }
        holder.binding.include.itemDesc.text = item.content
        holder.binding.include.itemAuthor.text = getString(R.string.class_notification_item_desc, item.author)
        holder.binding.include.itemLevel.text = item.levelText
        holder.binding.include.itemImage.setImageURI(item.picList?.getOrNull(0)?.url)
        holder.binding.root.setOnClickListener {
            if (item.status == 1) {
                item.status = 2
                notifyItemChanged(holder.adapterPosition)
                lifecycleScope.launch(CoroutineExceptionHandler { }) {
                    withContext(Dispatchers.IO) { ClassApi.service.updateNoticeStatus(item.relationId) }
                }
            }
            WebFragment.startWeb(requireActivity(), WebFragment.CLASS_NOTIFICATION.format(item.id))
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val tabs = resources.getStringArray(R.array.class_tabs)
        binding.tabLayout.tabs(tabs) { tab ->
            val text = binding.search.text.toString()
            when (tab) {
                getString(R.string.class_7_day) -> viewModel.load(type = 1, text = text, 1)
                getString(R.string.class_3_month) -> viewModel.load(type = 2, text = text, 1)
                getString(R.string.class_current_semester) -> viewModel.load(type = 3, text = text, 1)
                getString(R.string.class_all) -> viewModel.load(type = 0, text = text, 1)
            }
        }

        binding.recycler.adapter = adapter
        binding.search.doOnSearch {
            viewModel.load(text = it, page = 1)
            false
        }

        viewModel.data.observe(viewLifecycleOwner) {
            if (it.first == 1) {
                adapter.notifyChanged(it.second)
            } else {
                adapter.addAll(it.second)
            }
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden.not() && view != null) {
            viewModel.load(page = 1)
        }
    }
}
